<template>
  <div :class="{disabled:fileList.length>0}">
    <el-upload
      :on-preview="handlePictureCardPreview"
      :on-change="handleChange"
      :on-remove="handleRemove"
      :before-upload="handleBeforeUpload"
      :http-request="upload"
      list-type="picture-card"
      action="#"
      :limit="1"
      :file-list="fileList"
    >
      <i class="el-icon-plus"></i>
    </el-upload>
    <el-progress v-if="showPercent" :percentage="percentage" style="width:180px;"></el-progress>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" src="dialogImgUrl" alt="">
    </el-dialog>
  </div>
</template>

<script>
var COS = require('cos-js-sdk-v5')
var cos = new COS({
  SecretId: 'AKIDJURAfvwiGZdT4RZS9cqpZ4KL8gz2rRmr',
  SecretKey: 'sVsyoSt76qLbcNtBE1Ypqb6LJ1Ut65pL'
})
export default {
  components: {},
  props: [],
  data() {
    return {
      dialogImgUrl: '',
      dialogVisible: false,
      // fileList: [{ name: 'food.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100' }],
      fileList: [],
      showPercent: false,
      percentage: 0,
      uploading: false
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    handleRemove(file, fileList) {
      console.log(file, fileList)
      //   删除方式1 通过splice
      //   删除方式2 通过filter
      this.fileList = this.fileList.filter(t => t !== file)
    },
    handleChange(file, fileList) {
      // console.log('handleChange')
      // this.fileList.push(file)
      // const f=this.fileList.find(t=>t===file)
      // if(!if)this.fileList.push(file)
      // this.fileList=this.fileList.map(t=>t)
      this.fileList.push(file)
    },
    handlePictureCardPreview(file) {
      this.dialogImgUrl = file.url
      this.dialogVisible = true
    },
    handleBeforeUpload({ type, size }) {
      if (!['image/jpeg', 'image/gif', 'image/bmp', 'image/png'].includes(type)) {
        this.$message.error('图片类型不对')
        return false
      }
      if (size > 5 * 1024 * 1024) {
        this.$message.error('图片大小不对')
        return false
      }
      this.showPercent = true
      return true
    },
    upload({ file }) {
      this.uploading = true
      cos.putObject({
        Bucket: 'renzizhongtai-1302006452', /* 必须 */
        Region: 'ap-chengdu', /* 必须 */
        Key: Date.now() + file.name, /* 必须 */
        StorageClass: 'STANDARD',
        Body: file, // 上传文件对象
        onProgress: (progressData) => {
          this.percentage = progressData.percent * 100
        }
      }, (err, data) => {
        console.log(err || data)
        this.fileList[0].url = `http://${data.Location}`
        this.showPercent = false
        this.uploading = false
      })
    }
  }
}
</script>

<style lang="scss" scoped>
// .disabled .el-upload--picture-card[data-v-82a94682]
// .disabled .el-upload--picture-card{

//[data-v-82a94682] .disabled .el-upload--picture-card
// ::v-deep .disabled .el-upload--picture-card{}
::v-deep.disabled .el-upload--picture-card{
display: none !important;
}
</style>
